package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Student;

/*
库管类:
核心职责管理我们的数据仓库--数组或集合
这里的所有方法和功能都是给业务层用
 */
public class StudentDao implements BaseStudentDao {
    private static Student[] stu = new Student[5];

    static {
        Student stu1=new Student("heima001","张三","23","1997-10-10");
        Student stu2=new Student("heima002","李四","24","1996-10-10");
        stu[0]=stu1;
        stu[1]=stu2;
    }
    public boolean addStudent(Student student) {
        int index = -1;
        for (int i = 0; i < stu.length; i++) {
            if (stu[i] == null) {
                index = i;
                break;
            }
        }

        if (index == -1) {
            return false;
        } else {
            stu[index] = student;
            return true;
        }
    }

    public Student[] findAllStudent() {
        return stu;
    }

    public void deleteStudent(String delId) {
        int index = getIndex(delId);
        stu[index] = null;

    }

    public int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < stu.length; i++) {
            Student student = stu[i];
            if (student != null && student.getSid().equals(id)) {
                index = i;
            }
        }
        return index;
    }

    public void updateStudent(String updateId, Student newStu) {

        int index = getIndex(updateId);
        stu[index] = newStu;
    }
}
